package cn.ljy.uem.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyForbiddenException;
import cn.ljy.common.exception.model.base.MyRollBackException;
import cn.ljy.uem.dao.SchoolTeacherCourseMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

/**
 * 公共业务的实现类
 * @param <T> 模块的实体类
 * @param <Q> 模块的实体查询类
 */
@Service
@Validated
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Slf4j
public abstract class IBaseSchoolService<T,Q> extends IBaseService<T,Q> {
    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;
    @Autowired
    private SchoolTeacherCourseMapper schoolTeacherCourseMapper;
    /**
     * 验证教师是否拥有该课程的管理权限
     * @param courseId
     */
    private void validateManageCourseAuthorityOfLoginAuthority(Long courseId){
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            Long teacherId = schoolTeacherMapper.getIdByUserId(sysUser.getId());
            if(teacherId==null){
                throw new MyForbiddenException("操作失败：只有教师才可以操作");
            }
            if(schoolTeacherCourseMapper.countByCourseIdAndTeacherId(courseId,teacherId)==0){
                throw new MyForbiddenException("操作失败：您没有该课程的管理权限");
            }
        }
    }
}
